package com.longrise.android.byjk.plugins.tabfirst.qrcode;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.DZZWTools;
import com.longrise.android.byjk.utils.Tracker_Google;

/**
 * Created by callxkj on 16/8/4.
 * 二维码扫描确认登录界面
 */
public class QRconfirmActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvback;
    private TextView mTvconfirm;
    private TextView mTvcancel;
    private String qrCodeContent;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_qrconfirm;
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        qrCodeContent = intent.getStringExtra("qrCodeContent");
        mTvback = (TextView) findViewById(R.id.qrconfirm_back);
        mTvconfirm = (TextView) findViewById(R.id.qrconfirm_confirm);
        mTvcancel = (TextView) findViewById(R.id.qrconfirm_cancel);
        initEvent();

    }

    private void initEvent() {
        mTvconfirm.setOnClickListener(this);
        mTvback.setOnClickListener(this);
        mTvcancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qrconfirm_confirm: //确认登录
                confirmLogin();
                addEvent("code-login", "code-login-yes", "确认登录");

                break;
            case R.id.qrconfirm_back:  //关闭
                finish();
                break;
            case R.id.qrconfirm_cancel:  //取消登录
                addEvent("code-login", "code-login-no", "取消登录");
                finish();
                break;
        }
    }

    private void addEvent(String type, String aciton, String lable){
        Tracker_Google.getInstance().addEvent(type, aciton, lable);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Tracker_Google.getInstance().addGoogleScreenName("login-QR-code");
    }

    /**
     * 确认登录
     */
    private void confirmLogin() {
        String userid = UserInfor.getInstance().getUserid();
        String usersfzh = UserInfor.getInstance().getUsersfzh();
        EntityBean bean = new EntityBean();
        bean.set("qrCodeContent", qrCodeContent);
        bean.set("userID", userid);
        bean.set("cardno",usersfzh);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        DZZWTools.showWaitDialog(this);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_login_ensureLogin", fbean, new LoadDataManager.OnRequestCallListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    Integer resultstate = bean.getInt("resultstate");
                    if (resultstate == 1) {
                        Intent intent = new Intent(QRconfirmActivity.this, QRloginsuccessActivity.class);
                        startActivity(intent);
                        finish();
                        addEvent("code-login", "Success-code", "扫码登录成功");
                    } else {
                        String resultdesc = bean.getString("resultdesc");
                        DZZWTools.showToast(QRconfirmActivity.this, resultdesc, 0);
                    }
                } catch (Exception e) {
                    DZZWTools.showToast(QRconfirmActivity.this, "登录失败", 0);
                }
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                DZZWTools.showToast(QRconfirmActivity.this, "登录失败", 0);
            }

            @Override
            public void onFinished(String key, String service) {
                DZZWTools.dismissDialog(null);
            }
        });


    }
}
